package org.java.string;

import org.junit.Test;

/**
 * 字符串操作
 * Created by Administrator on 2016/10/13.
 */
public class StringTest {

    /**
     * 测试截取字符串
     */
    @Test
    public void test1() {
        String str = "D:/tools/apache-tomcat-7.0.70/webapps/bigdtp/";
        int t1 = getIndexDesc(str, '/', 2);
        String t2 = str.substring(0, t1);
        System.out.println(t1);
        System.out.println(t2);
        System.out.println(str.length());
    }

    /**
     * 获取字符串指定字符出现的位置，倒序查找
     * @param str 字符串
     * @param ch 指定字符
     * @param index 要求第几次出现的位置
     * @return -1表示没有查到
     * */
    public int getIndexDesc(String str, char ch, int index) {
        int number = 0;
        char arr[] = str.toCharArray();
        for (int i = arr.length-1; i >= 0; i--) {
            if (arr[i] == ch) {
                number++;
            }
            if (number == index) {
                return i;
            }
        }
        return -1;
    }
}
